package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.pojo.DishCash;
import com.itheima.dao.OrderDetailDao;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.OrderDetail;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/statics")
public class StaticsController {
    @Autowired
    CategoryService categoryService;

    @Autowired
    SetmealService setmealService;

    @Autowired
    DishService dishService;

    @Autowired
    OrderDetailDao orderDetailDao;

    /**
     * 菜系营收占比分析
     */
    @GetMapping("/dishCash")
    public R selectDishCash() throws InterruptedException {
        ArrayList<DishCash> list = new ArrayList<>();
        //获取订单详情数据
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();

        wrapper.isNull(OrderDetail::getSetmealId);
        List<OrderDetail> orderDetailList = orderDetailDao.selectList(wrapper);
        for (OrderDetail orderDetail : orderDetailList) {
            //获取每个订单的dishId去查找分类
            LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
            qw.eq(Dish::getId,orderDetail.getDishId());
            Dish dishOne = dishService.getOne(qw);
            //通过CategoryId去获取分类名称
            LambdaQueryWrapper<Category> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(Category::getId,dishOne.getCategoryId());
            Category categoryOne = categoryService.getOne(qw2);
            //分类名
            String name = categoryOne.getName();
            //总价
            BigDecimal amount = orderDetail.getAmount();


            boolean flag = true;   //没有相同分类的订单，直接存到对象中
            if(list.size()!=0){
                for (DishCash cash : list) {
                    if(name.equals(cash.getName())){
                        flag = false;
                        //有相同分类的订单，价格累加
                        break;
                    }                }
            }
            if(flag){   //没有相同分类的订单,直接赋值给对象，存到集合中
                //用DishCash对象存储数据
                DishCash dishCash = new DishCash();
                dishCash.setValue(amount);
                dishCash.setName(name);
                list.add(dishCash);
            }else {
                //有相同分类的订单，价格累加
                for (DishCash dish : list) {    //找到相同分类的对象，重新赋值
                    if (name.equals(dish.getName())){
                        dish.setName(name);
                        dish.setValue(amount.add(dish.getValue()));
                    }
                }
//                for (int i = 0; i < list.size(); i++) {
//                    if (name.equals(list.get(i).getName())){
//                        list.get(i).setName(name);
//                        list.get(i).setValue(amount.add(list.get(i).getValue()));
//                    }
//                }
            }
        }
        return R.success(list);
    }


    /**
     * 月营业额趋势图
     */
//    @GetMapping("/cashPerMonth")
//    public R selectCashPerMonth(){
//
//    }
}
